package com.leyou.item.service.impl;

import com.leyou.item.interfaces.pojo.Category;
import com.leyou.item.mapper.CategoryMapper;
import com.leyou.item.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * CategoryService接口的实现类, 主要业务逻辑
 * @version 1.0.0
 * @Author yong.Mr
 * @data 2019-8-9
 */
@Service
@Transactional
public class CategoryServiceImpl implements CategoryService {
    /**
     * 注入CategoryMapper
     */
    @Autowired
    private CategoryMapper categoryMapper;
    /**
     * 查询该父目录下的所有文件和目录
     * @param pid parent_id 父类目id
     * @return List<Category>
     */
    @Override
    public List<Category> queryCategoryByPid(Long pid) {
        // 根据set了的字段进行查询
        Category category = new Category();
        category.setParentId(pid);
        // 根据parent_id == pid进行查询
        List<Category> categoryByParentId = categoryMapper.select(category);
        return categoryByParentId;
    }

    /**
     * 根据品牌Id查询商品分类
     * @param bid 品牌id
     * @return List<Category>
     */
    @Override
    public List<Category> queryByBrandId(Long bid) {
        List<Category> categories = categoryMapper.queryByBrandId(bid);
        return categories;
    }

    /**
     *
     * @param ids
     * @return
     */
    @Override
    public List<String> queryNamesByIds(List<Long> ids) {
        Example example = new Example(Category.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIn("id", ids);
        List<Category> categories = categoryMapper.selectByExample(example);
        System.out.println("查询出来的商品分类的数据----->:" + categories);

        List<String> names = new ArrayList<>();
        categories.forEach(category->{
            names.add(category.getName());
        });
        return names;
    }

    /**
     * 根据3级分类id，查询1~3级的分类
     * @param id
     * @return
     * */
    @Override
    public List<Category> queryAllByCid3(Long id) {
        Category c3 = this.categoryMapper.selectByPrimaryKey(id);
        Category c2 = this.categoryMapper.selectByPrimaryKey(c3.getParentId());
        Category c1 = this.categoryMapper.selectByPrimaryKey(c2.getParentId());
        return Arrays.asList(c1,c2,c3);
    }
}
